<?php
namespace app\daifast\controller; 
use \think\Db;


class Manage extends \think\Controller
{
    //管理员列表 
    public function adminList(){
        $where = ' 1=1 ';
        $adminName = addslashes(input('adminName')); 
        $adminName and $where .= 'and username = "'.$adminName.'"';
        $data = Db::table('sys_admin')->where($where)->order('id desc')->paginate(10,['query'=>['username'=>$adminName]]);
        $adminArr = $data->items(); 
        $pages = $data->render();
        //超级管理员或管理员
        $group = Db::table('sys_admin_group')->select();
        $group_arr = array_column($group, 'names', 'id');
        return $this->allFetch(get_defined_vars());
    }
    
    //管理员编辑或添加
    public function adminAdd(){
        //超级管理员或管理员
        $group = Db::table('sys_admin_group')->select();
        $group_arr = array_column($group, 'names', 'id');
        //获取编辑页详情
        input('admin_id') and $admin_id = input('admin_id');
        $admin_id and $info = Db::table('sys_admin')->where("id=".intval(input('admin_id') ))->find();
        if(input('sbt')){
            if(!input('admin_id') && input('username')){
                $res = Db::table('sys_admin')->where('username', input('username'))->find();
                $res and $this->showMsg("该管理员已经存在，请重新添加！", url('Manage/adminAdd'));
            }
            input('admin_id') and $admin_id = input('admin_id');
            input('group_id') and $data['group_id'] = input('group_id');
            $data['password'] = md5(input('password'));
            $data['on_time'] = time();
            $data['username'] = input('username');
            $data['truename'] = input('truename');
            $data['mobile'] = input('mobile'); 
            $data['qq'] = input('qq');
            
            if($admin_id){ 
                Db::table('sys_admin')->where('id',intval($admin_id))->update($data); 
            }else{
                Db::table('sys_admin')->insert($data);
            }
            $this->showMsg("操作成功", url('Manage/adminList'));
        }
        return $this->allFetch(get_defined_vars());
    }
    
    //管理员删除
    public function delete(){
        Db::table('sys_admin')->where('id', input('admin_id'))->delete();
        echojson("ok", 1);
    }
    
    //管理员激活或冻结
    public function setstatus(){
        Db::table('sys_admin')->where('id', input('admin_id'))->update(['status' => input('status')]);
        echojson("ok", 1, input('status'));
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}
